package shell

import (
	"fmt"
	"strings"
)

// Ls executes "ls" command with args (if passed) on device
func (d DeviceShell) Ls(lsPath string, args ...string) ([]byte, error) {
	fc := append(args, lsPath)
	return d.RunSync("ls", fc...)
}

// MkDir executes "mkdir" with args (if passed) on device
func (d DeviceShell) MkDir(dirPath string, args ...string) ([]byte, error) {
	fc := append([]string{dirPath}, args...)
	return d.RunSync("mkdir", fc...)
}

// Rm executes "rm" with args (if passed) on device
func (d DeviceShell) Rm(object string, args ...string) ([]byte, error) {
	fc := append([]string{object}, args...)
	return d.RunSync("rm", fc...)
}

// OrchestratorClassMethod is like InstrumentClassMethod but with Orchestrator
func (d DeviceShell) OrchestratorClassMethod(classMethod string, runner string) ([]byte, error) {
	return d.OrchestratorClassMethodParams(nil, classMethod, runner)
}

// OrchestratorClassMethodParams is like InstrumentClassMethodParams but with Orchestrator
func (d DeviceShell) OrchestratorClassMethodParams(params map[string]string, classMethod string, runner string) ([]byte, error) {
	var args []string
	for k, v := range params {
		args = append(args, "-e", k, v)
	}

	return d.RunSync(fmt.Sprintf("CLASSPATH=$(pm path androidx.test.services) app_process / "+
		"androidx.test.services.shellexecutor.ShellMain am instrument -w %s -e class %s %s/androidx.test.runner.AndroidJUnitRunner "+
		"androidx.test.orchestrator/.AndroidTestOrchestrator", strings.Join(args, " "), classMethod, runner))
}
